import LXCOM from '../../common/common.js'
import abspkg from '../../graph/action/absaction.js'
import {LGXEnums} from '../../common/sysenum.js'

import UtilFuncs from '../../common/tools/utilfuncs.js'
var utilfuncs = UtilFuncs.UtilFuncs;

class BezierCurveAction extends abspkg.AbsGraphAction {
    constructor(graph) {
        super(graph);
    }

    act2Mouse(mosEvnt, mousePt) {
        var graph = this.getRefGraph();
        if (graph == null) {
            return LGXEnums.LGXPlotState.ErrorState;
        }
        var state = LGXEnums.LGXPlotState.NoneState;
        var position = graph.getPos();
        if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseDown) {
            if (graph.getStart() == 0) {
                graph.setStart(1);
                var p1 = new LXCOM.LGXPoint();
                p1.x = mousePt.x;
                p1.y = mousePt.y;
                var p2 = new LXCOM.LGXPoint();
                p2.x = mousePt.x;
                p2.y = mousePt.y;
                position.posList.push(p1);
                position.posList.push(p2);
                state = LGXEnums.LGXPlotState.AppendPtState;
            }
            else if (position.posList.length == 4 && graph.getFinish() == 0) {
                var p = position.posList[3];
                p.x = mousePt.x;
                p.y = mousePt.y;
                graph.setFinish(1);
                state = LGXEnums.LGXPlotState.FinistState;
            }
            else {
                var poscount = position.posList.length;

                var validNewPos = true;
                if (poscount > 1) {
                    var prePrePt = position.posList[poscount - 2];
                    var dis = utilfuncs.distancePoint(prePrePt, mousePt, 1);
                    if (dis < 1) {
                        validNewPos = false;
                    }
                }

                if (validNewPos) {
                    var prePt = position.posList[poscount - 2];
                    var p = new LXCOM.LGXPoint();
                    var horn_vert_kind = utilfuncs.chkHornVertKind(mousePt, prePt);
                    if (horn_vert_kind == 0) {
                        p.x = mousePt.x;
                        p.y = mousePt.y;
                    }
                    else if (horn_vert_kind == 1) {
                        p.x = mousePt.x;
                        p.y = prePt.y;
                    }
                    else if (horn_vert_kind == 2) {
                        p.x = prePt.x;
                        p.y = mousePt.y;
                    }
                    position.posList.push(p);
                    state = LGXEnums.LGXPlotState.AppendPtState;
                }
            }

        }
        else if (mosEvnt.type == LGXEnums.LGXMouseMode.mouseMove
            || mosEvnt.type == LGXEnums.LGXMouseMode.mouseDrag) {
            var lst = position.posList;
            var idx = lst.length - 1;
            var p11 = lst[idx];

            poscount = position.posList.length;
            prePt = position.posList[poscount - 2];
            horn_vert_kind = utilfuncs.chkHornVertKind(mousePt, prePt);
            if (horn_vert_kind == 0) {
                p11.x = mousePt.x;
                p11.y = mousePt.y;
            }
            else if (horn_vert_kind == 1) {
                p11.x = mousePt.x;
                p11.y = prePt.y;
            }
            else if (horn_vert_kind == 2) {
                p11.x = prePt.x;
                p11.y = mousePt.y;
            }

            state = LGXEnums.LGXPlotState.OKState;
        }
        graph.calScope(1);
        return state;
    }
}

export default { BezierCurveAction }
